package com.soarsky.boteng;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.sccl.common.util.DevLog;
import cn.sccl.common.util.StringUtil;

public class ScaffoldBuilder {
	protected final Log logger = LogFactory.getLog(getClass());

	protected final static String PKG_PREFIX = "com.soarsky.boteng.";
	protected final static String PKG_SUFFIX_MODEL = "model.";
	protected final static String PKG_SUFFIX_DAO = "dao.";
	protected final static String PKG_SUFFIX_MANAGER = "service.";
	protected final static String PKG_IMPL = "impl";
	protected final static String PKG_SUFFIX_ACTION = "action.";
	protected final static String PKG_SUFFIX_APP = "app.";
	protected final static String PKG_SUFFIX_WEB = "web.";
	protected final static String PKG_SUFFIX_CONTROLLER = "controller.";
	protected final static String PKG_SUFFIX_MAPPER = "mapper.";
	protected final static String MAPPER_SUFFIX="-mapper.xml";
	protected final static String DAO_SUFFIX="DAO";
	protected final static String SERVICE_SUFFIX="Service";
	protected final static String ACTION_SUFFIX="Action";
	protected final static String CONTROLLER_SUFFIX="Controller";
	
	protected final static String AUTHOR="鲜虎";
	
	protected String pkgName;//模块基本包路径
	protected String clzName;
	protected TableInfo tableInfo;
	private final Map<String, String> mapping;//替换标签键值对

	public ScaffoldBuilder(String pkgName, String clzName, TableInfo tableInfo) {
		this.pkgName = PKG_PREFIX + pkgName;
		this.clzName = clzName;
		this.tableInfo = tableInfo;

		mapping = new HashMap<String, String>();

		mapping.put("clzName", clzName);//类名(PO)
		mapping.put("clzNameLC", StringUtils.uncapitalize(clzName));//类名(PO首字母小写)
		mapping.put("tblName", tableInfo.getName());//表名
		mapping.put("modelPath", getModelPath());
		mapping.put("daoPath", getDaoPath());
		//mapping.put("mapperPath", getMapperPath());
		//mapping.put("daoImplPath", getDaoImplPath());
		mapping.put("managerPath", getManagerPath());
		mapping.put("managerImplPath", getManagerImplPath());
		mapping.put("actionPath", getActionPath());
		mapping.put("controllerPath", getControllerPath());
		//PO的属性
		mapping.put("fieldsDeclareInfo", tableInfo.getFieldsDeclareInfo());
		//结果集配置映射文件
		mapping.put("resultMap", tableInfo.getResultMap());
		//其他条件配置映射文件
		mapping.put("otherCondition", tableInfo.getOtherCondition());		

		DevLog.debug(tableInfo.getPrimaryKey());
		mapping.put("primaryKey", tableInfo.getPrimaryKey());
		
		DevLog.debug(tableInfo.getParserKey());
		mapping.put("parserKey", tableInfo.getParserKey());
		
		mapping.put("UNparserKey",StringUtils.capitalize(tableInfo.getParserKey()));
		
		DevLog.debug(tableInfo.getFindByLike());
		mapping.put("findLikeBy", tableInfo.getFindByLike());
		
		DevLog.debug(tableInfo.getSelectStatement());
		mapping.put("selectStatement", tableInfo.getSelectStatement());
		
		DevLog.debug(tableInfo.getInsertStatement());
		mapping.put("insertStatement", tableInfo.getInsertStatement());
		
		DevLog.debug(tableInfo.getUpdateStatement());
		mapping.put("updateStatement", tableInfo.getUpdateStatement());

		mapping.put("updateMapModel", tableInfo.getUpdateMapModel());
		mapping.put("author",AUTHOR);
	}
	/**
	 * PO类路径
	 * @return
	 */
	public String getModelPath() {
		return PKG_PREFIX + PKG_SUFFIX_MODEL + clzName;
	}
    /**
     * sql配置文件生成路径文件名
     * @return
     */
	public String getSqlMapFile() {
		return pkgName + PKG_SUFFIX_WEB +PKG_SUFFIX_MODEL + clzName +MAPPER_SUFFIX;
	}
    /**
     * DAO接口路径
     * @return
     */
	public String getDaoPath() {
		return pkgName + PKG_SUFFIX_APP + PKG_SUFFIX_DAO + clzName + DAO_SUFFIX;
	}
    /**
     * DAO实现路径
     * @return
     */
	public String getDaoImplPath() {
		return pkgName + PKG_SUFFIX_APP + PKG_SUFFIX_DAO + PKG_IMPL + StringUtil.DOT + clzName + DAO_SUFFIX
				+ StringUtils.capitalize(PKG_IMPL);
	}
    /**
     * Service类路径
     * @return
     */
	public String getManagerPath() {
		return pkgName + PKG_SUFFIX_APP + PKG_SUFFIX_MANAGER + clzName + SERVICE_SUFFIX;
	}
    /**
     * Service实现类路径
     * @return
     */
	public String getManagerImplPath() {
		return pkgName + PKG_SUFFIX_APP + PKG_SUFFIX_MANAGER + PKG_IMPL + StringUtil.DOT + clzName + "Service"
				+ StringUtils.capitalize(PKG_IMPL);
	}
	/**
	 * Action类类路径
	 * @return
	 */
	public String getActionPath() {
		return pkgName + PKG_SUFFIX_APP + PKG_SUFFIX_ACTION + clzName + ACTION_SUFFIX;
	}
	/**
	 * Controller类路径
	 * @return
	 */
	public String getControllerPath(){
		return pkgName + PKG_SUFFIX_WEB+PKG_SUFFIX_CONTROLLER+clzName+CONTROLLER_SUFFIX;
	}
    /**
     * 需要生成的文件列表
     * @return
     */
	public List<FileGenerator> buildGenerators() {
		List<FileGenerator> list = new ArrayList<FileGenerator>();
		
		// dao
		list.add(new FileGenerator(pkgName + "app.dao", clzName + "DAO", "DAO.txt", mapping));
		
		// Service
		list.add(new FileGenerator(pkgName + "app.service", clzName + "Service", "Service.txt", mapping));
		list.add(new FileGenerator(pkgName + "app.service.impl", clzName + "ServiceImpl", "ServiceImpl.txt", mapping));
		
		//配置文件
		list.add(new FileGenerator(pkgName + "app.service", clzName+"service", "XMLSERVICE.txt", mapping, "xml"));
		//list.add(new FileGenerator(pkgName + "app.service", clzName+"dao", "XMLDAO.txt", mapping, "xml"));
		list.add(new FileGenerator(pkgName + "app.action", clzName+"action", "XMLACTION.txt", mapping, "xml"));
		
		//Action
		list.add(new FileGenerator(pkgName + "app.action", clzName + "Action", "Action.txt", mapping));

		// model(PO)
		list.add(new FileGenerator(PKG_PREFIX + "model", clzName, "Model.txt", mapping));
		
		//mybatisSQL映射配置文件
		list.add(new FileGenerator(PKG_PREFIX + "model", clzName+"-mapper", "SqlMap.txt", mapping, "xml"));
		
		// controller
		list.add(new FileGenerator(pkgName + "web.controller", clzName + "Controller", "Controller.txt", mapping));
		list.add(new FileGenerator(pkgName + "web.controller", clzName + "Controller", "XMLCONTROLLER.txt", mapping,"xml"));
		
		//list.add(new FileGenerator(pkgName + "web.validator", clzName + "Validator", "Validator.txt", mapping));
		return list;
	}
public static void main(String[] args){
   System.out.println(StringUtils.capitalize("xianhu"));
}
}
